package com.delta.service.houseInfo.impl;

import com.delta.bean.houseInfo.HouseInfoBean;
import com.delta.mybaits.model.houseInfo.HouseInfoModel;
import com.delta.service.houseInfo.HouseInfoService;
import com.delta.mybaits.mapper.HouseInfoMapper;
import com.delta.base.BaseService;
import com.delta.tools.DateUtil;
import com.delta.tools.IDGeneratorUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.apache.log4j.Logger;


/**
 *<pre>
 *  HouseInfo service 实现类
 *  通过 lion generator 生成，禁止使用商业
 *  时间: 2017-57-19 14:57:21
 *</pre>
 * @author lion
 * @version 1.0
**/

@Service
public class HouseInfoServiceImpl extends BaseService implements HouseInfoService{

	private static Logger logger = Logger.getLogger(HouseInfoServiceImpl.class);

	@Autowired
	private HouseInfoMapper houseInfoMapper;

	/**列表**/
	@Override
	public PageInfo getHouseInfoListByBean(HouseInfoBean bean){
		logger.info("getHouseInfoListByBean bean : " +  bean.toString());

		HouseInfoModel model = new HouseInfoModel();

		Page pageInfo = super.setPageNumber();
		houseInfoMapper.findHouseInfoListByBean(bean);
		return pageInfo.toPageInfo();
	}

	 /**根据id获取bean对象**/
	@Override
	public HouseInfoBean getHouseInfoBeanById(HouseInfoBean bean){
		HouseInfoModel model = houseInfoMapper.selectByPrimaryKey(bean.getId());
		HouseInfoBean newBean = new HouseInfoBean();
		BeanUtils.copyProperties(model, newBean);
		if(!StringUtils.isEmpty(model.getCreateTime())){
			newBean.setCreateTime(DateUtil.toStringForThree(model.getCreateTime()));
		}
		 return newBean;
	}

	/**新增 或修改**/
	@Override
	public void addOrModifyHouseInfoByBean(HouseInfoBean bean){
		logger.info("addOrModifyHouseInfoByBean bean  : "   +  bean.toString());

		HouseInfoModel model = new HouseInfoModel();
		BeanUtils.copyProperties(bean, model);
		 String uuid = null;
		 uuid = IDGeneratorUtil.getUUID();
		if(!StringUtils.isEmpty(bean.getCreateTime())){
			model.setCreateTime(DateUtil.toDate(bean.getCreateTime()));
		}
		if(model.getId() != null){
			houseInfoMapper.updateByPrimaryKey(model);
		}else{
			if(uuid != null){
				model.setId(uuid);
			}
			houseInfoMapper.insert(model);
		}
	}

	/**删除**/
	@Override
	public void deleteHouseInfoByBean(HouseInfoBean bean){
		logger.info("deleteHouseInfoByBean bean : "  + bean.toString());

		HouseInfoModel model = new HouseInfoModel();
		BeanUtils.copyProperties(bean, model);
		if(model.getId() != null){
			houseInfoMapper.deleteByPrimaryKey(model);
		}
	}
}
